package com.clouds.wms.inv;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.bean.Invma;
import com.clouds.wms.bean.Invmb;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmjService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.service.InvmaService;
import com.clouds.wms.service.InvmbService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;
@Controller
@RequestMapping("/invmb")
public class InvmbController {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="invmaService")
	private InvmaService invmaService;
	@Resource(name="invmbService")
	private InvmbService invmbService;
	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;
	@Resource(name="cmsmjService")
	private CmsmjService cmsmjService;
	@RequestMapping(value = "/invmbMain.xhtml")
	public String invmbMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String maaa = RequestHelper.getString(request, "maaa");
		String maab = RequestHelper.getString(request, "maab");
		Invma invma = invmaService.getInvma(maaa, maab);
		model.addAttribute("maaa", maaa);
		model.addAttribute("maab", maab);
		model.addAttribute("invma", invma);
		return "inv/invmb";
	}
	@RequestMapping(value = "/invmbList.xhtml")
	public String invmbList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String maaa = RequestHelper.getString(request, "maaa");
		String maab = RequestHelper.getString(request, "maab");
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = invmbService.getInvmbList(maaa, maab, pageNumber, pageSize);
		List<Invmb> list = (List<Invmb>) pb.getData();
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 添加采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/addInvmb.xhtml", method = RequestMethod.POST)
	public String addInvma(HttpServletRequest request, HttpServletResponse response, Model model, Invmb invmb) throws SysException, IOException {
	
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		invmb.setMbac(invmbService.getMaxMbac());
		invmbService.saveInvmb(invmb);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + invmb.getMbab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 更新采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/updateInvmb.xhtml", method = RequestMethod.POST)
	public String updateInvma(HttpServletRequest request, HttpServletResponse response, Model model, Invmb invmb) throws SysException, IOException {
	
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		invmbService.updateInvmb(invmb);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("更新了编号为[" + invmb.getMbab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delInvmb.xhtml")
	public String delInvmb(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mbaa = RequestHelper.getString(request, "mbaa");
		String mbab = RequestHelper.getString(request, "mbab");
		String mbac = RequestHelper.getString(request, "mbac");
		StringBuffer mbacs = new StringBuffer();
		for(String s : mbac.split(",")){
			mbacs.append("'").append(s).append("',");
		}
		mbacs = mbacs.delete(mbacs.length()-1, mbacs.length());
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		invmbService.delInvmb(mbaa, mbab, mbacs.toString());
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + mbaa+","+mbab+","+mbac + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delInvmbAll.xhtml")
	public String delInvmbAll(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mbaa = RequestHelper.getString(request, "mbaa");
		String mbab = RequestHelper.getString(request, "mbab");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		invmbService.delInvmbAll(mbaa, mbab);
		out.print("success");
		return null;
	}
	/**
	 * 变更采购单状态
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/auditInvmb.xhtml", method = RequestMethod.POST)
	public String auditInvmb(HttpServletRequest request, HttpServletResponse response, Model model, Invmb invmb) throws SysException, IOException {
		String mbaa = RequestHelper.getString(request, "maaa");
		String mbab = RequestHelper.getString(request, "maab");
		String status = RequestHelper.getString(request, "flag");
//		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		invmbService.changeStatus(mbaa, mbab, status);
		
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delInvmamb.xhtml", method = RequestMethod.POST)
	public String delInvmamb(HttpServletRequest request, HttpServletResponse response, Model model, Invmb invmb) throws SysException, IOException {
		String mbaa = RequestHelper.getString(request, "maaa");
		String mbab = RequestHelper.getString(request, "maab");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		invmbService.delInvmamb(mbaa, mbab);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + invmb.getMbab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
}
